# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from solve_q2 import risk_analyze

def series_risk(x):
    '''
    30 分钟为一次，计算风险
    '''
    num = len(x)
    risk_list = []
    for i in range(num//60+1):
        if i != num//60:
            x_60 = x[60*i:60*(i+1)]
        else:
            x_60 = x[60*i:]
        risk = risk_analyze(x_60)
        risk_list.append(risk)
        
    return risk_list
    
def solve_q3(data):
    '''
    对每个感应器，按 30 分钟为一次，计算风险
    '''
    cols = data.columns
    sensor_risk = {}
    for col in cols:
        x = data[col]
        x = x.values
        risk = series_risk(x)
        sensor_risk[f'感应器{col}'] = risk 
    
    time_index = range(1, 93)
    
    table = pd.DataFrame.from_dict(sensor_risk, orient='index')
    table.columns = time_index
    table.to_excel(r'../附件/中间数据/时间-风险-15分钟.xlsx')
    return table
    
if __name__ == '__main__':
    path = r'../附件/附件1(Appendix 1)2021-51MCM-Problem C.xlsx'
    data = pd.read_excel(path, index_col=1, header=0, parse_dates=True)
    
    data.drop(data.columns[0], axis=1, inplace=True)
    solve_q3(data)
    
    

    

